package com.atguigu.hash;

import com.atguigu.array.Array;
import com.atguigu.map.AVLMap;
import com.atguigu.map.BSTMap;

import com.atguigu.set.FileOperation;
import com.atguigu.tree.AVLTree;
import com.atguigu.tree.RBTree;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;

public class HashTableTest {
    public static void main(String[] args) {
        System.out.println("Pride and Prejudice");
        ArrayList<String> words = new ArrayList<>();
        String filename = "pride-and-prejudice.txt";
        if (FileOperation.readFile(filename, words)) {
            System.out.println("Total words：" + words.size());
            System.out.println("==================================================");
            long startTime = System.nanoTime();
            BSTMap<String, Integer> bstMap = new BSTMap<>();
            for (String word : words) {
                Integer count = bstMap.get(word);
                bstMap.add(word, count == null ? 1 : count + 1);
            }
            for (String word : words) {
                bstMap.contains(word);
            }
            System.out.println("BSTree: " + bstMap.getSize());
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("BST: " + time + " s");
            System.out.println("==================================================");


            long startTime1 = System.nanoTime();
            AVLTree<String, Integer> avlTree = new AVLTree<>();
            for (String word : words) {
                if (avlTree.contains(word)) {
                    avlTree.set(word, avlTree.get(word) + 1);
                } else {
                    avlTree.add(word, 1);
                }
            }
            for (String word : words) {
                avlTree.contains(word);
            }
            System.out.println("AVLTree:  " + avlTree.getSize());
            long endTime1 = System.nanoTime();
            double time1 = (endTime1 - startTime1) / 1000000000.0;
            System.out.println("AVLTree: " + time1 + " s");
            System.out.println("==================================================");

            long startTime2 = System.nanoTime();
            RBTree<String, Integer> brt = new RBTree<>();
            for (String word : words) {
                if (brt.contains(word)) {
                    brt.set(word, brt.get(word) + 1);
                } else {
                    brt.add(word, 1);
                }
            }
            for (String word : words) {
                brt.contains(word);
            }
            System.out.println("RBTree:  " + brt.getSize());
            long endTime2 = System.nanoTime();
            double time2 = (endTime2 - startTime2) / 1000000000.0;
            System.out.println("RBTree: " + time2 + " s");
            System.out.println("==================================================");

            long startTime3 = System.nanoTime();
            HashTable<String, Integer> hashTable = new HashTable<>();
            for (String word : words) {
                if (hashTable.contains(word)) {
                    hashTable.set(word, hashTable.get(word) + 1);
                } else {
                    hashTable.add(word, 1);
                }
            }
            for (String word : words) {
                hashTable.contains(word);
            }
            System.out.println("HashTable:  " + hashTable.getSize());
            long endTime3 = System.nanoTime();
            double time3 = (endTime3 - startTime3) / 1000000000.0;
            System.out.println("HashTable: " + time3 + " s");
        }
    }

}
